#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

int n, m;
int a[N][N];
int b[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void bfs() {
  queue<pii> q;
  rep(i, 1, n) rep(j, 1, m) {
    if (a[i][j] == 1) {
      q.push(pii(i, j));
      b[i][j] = 0;
    } else {
      b[i][j] = 1e9;
    }
  }
  while (q.size()) {
    pii now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    rep(i, 0, 3) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
      if (b[xx][yy] > b[x][y] + 1) {
        q.push(pii(xx, yy));
        b[xx][yy] = b[x][y] + 1;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, m) {
    char c;
    cin >> c;
    a[i][j] = c - '0';
  }
  bfs();
  rep(i, 1, n) {
    rep(j, 1, m) cout << b[i][j] << ' ';
    cout << endl;
  }
  return 0;
}